#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    directory="../fichiers";
    ui->setupUi(this);

    ui->connectButton->setVisible(true);
    ui->newCompte->setVisible(true);
    ui->requete->setVisible(false);
    ui->searchButton->setVisible(false);
    ui->ListButton->setVisible(false);
    ui->downloadButton->setVisible(false);
    ui->searchButton2->setVisible(false);
    ui->searchEdit->setVisible(false);
    ui->descButton->setVisible(false);
    ui->upButton->setVisible(false);

    ui->LE_password->setEchoMode(QLineEdit::Password);
    ui->LE_password->setInputMethodHints(Qt::ImhHiddenText| Qt::ImhNoPredictiveText|Qt::ImhNoAutoUppercase);

    ui->fileList->setVisible(false);
    ui->fileList->setSelectionBehavior(QAbstractItemView::SelectRows);
    setWindowTitle(tr("Client"));

}

MainWindow::~MainWindow()
{
    conex->logout();
    delete ui;
}

void MainWindow::on_actionServeur_triggered()
{
    ServerInfo m(this);
    m.exec();

}


void MainWindow::on_connectButton_clicked()
{


    if(ui->LE_password->text().isEmpty() || ui->LE_username->text().isEmpty())
    {
        message.critical(0,"Error","UserName ou Password non renseigne");
    }
    else
    {
        /**
          Recuperation des infos serveur
          */
        username=ui->LE_username->text().toStdString();
        QByteArray passwordHash = ui->LE_password->text().toStdString().c_str();
        password=QCryptographicHash::hash(passwordHash, QCryptographicHash::Sha1).toHex().constData();

        ifstream infoFile("config.ini", ios::in);  // on ouvre en lecture

        if(infoFile)
        {
            getline(infoFile, IP);
            getline(infoFile,PORT);
            getline(infoFile,dlPORT);
            getline(infoFile,directory);
            infoFile.close();


        }
        else
        {
            ServerInfo m(this);
            m.exec();

            ifstream infoFile("config.ini", ios::in);  // on ouvre en lecture
            getline(infoFile, IP);
            getline(infoFile,PORT);
            getline(infoFile,dlPORT);
            getline(infoFile,directory);
            infoFile.close();

        }
       try
       {
           /**
            *Creation connexion + envoi liste de fichiers
            **/
           conex = new Connexion(IP,PORT,dlPORT,username,password,directory,ui->newCompte->isChecked(),this);



           conex->sendFilesList(directory);

           ui->newCompte->setVisible(false);
           ui->l_username->setVisible(false);
           ui->l_password->setVisible(false);
           ui->LE_username->setVisible(false);
           ui->LE_password->setVisible(false);
           ui->connectButton->setVisible(false);

           ui->requete->setVisible(true);
           ui->searchButton->setVisible(true);
           ui->ListButton->setVisible(true);


       }
       catch(SocketException e)
       {
           message.critical(0,"Error",e.what());
       }
       catch(int e)
       {
            message.critical(0,"Error","Combinaison Username/password invalide");
       }

   }

    }



void MainWindow::on_ListButton_clicked()
{
    ui->searchButton->setVisible(false);
    ui->requete->setVisible(false);
    ui->ListButton->setVisible(false);


    vector<FileDescServ>fichiers = conex->getFilesList();

    fillGrid(fichiers);


}

void MainWindow::fillGrid(vector<FileDescServ>fichiers)
{
    QTableWidgetItem *newItem ;

    for(unsigned int i = 0; i<fichiers.size();i++)
    {
        ui->fileList->insertRow(i);
        newItem = new QTableWidgetItem(fichiers[i].name);
        ui->fileList->setItem(i,0,newItem);
        newItem = new QTableWidgetItem(fichiers[i].author);
        ui->fileList->setItem(i,1,newItem);
        newItem = new QTableWidgetItem(QString::number(fichiers[i].size));
        ui->fileList->setItem(i,2,newItem);

    }

    ui->l_username->setVisible(false);
    ui->l_password->setVisible(false);
    ui->LE_username->setVisible(false);
    ui->LE_password->setVisible(false);
    ui->connectButton->setVisible(false);
    ui->fileList->setVisible(true);
    ui->downloadButton->setVisible(true);
    ui->searchButton2->setVisible(true);
    ui->searchEdit->setVisible(true);
    ui->descButton->setVisible(true);
    ui->upButton->setVisible(true);

}

void MainWindow::on_downloadButton_clicked()
{
    string name = ui->fileList->item(ui->fileList->currentRow(),0)->text().toStdString();
    string author = ui->fileList->item(ui->fileList->currentRow(),1)->text().toStdString();
    DLWindow *dl = new DLWindow(this,name);
    dl->show();
    dl->launch(conex, name, author,directory);
    /**
     *Lancer le DL
     *
     */
}


void MainWindow::on_searchButton_clicked()
{
    if(ui->requete->text().isEmpty())
    {
        message.critical(0,"Erreur","Requete vide");
    }
    else
    {
        string requete = ui->requete->text().toStdString();

        string mot = ui->searchEdit->text().toStdString();
        vector<FileDescServ>fichiers=conex->searchDesc(mot);
       ui->fileList->setRowCount(0);
       fillGrid(fichiers);


    }

}




void MainWindow::on_actionDossier_triggered()
{
    QString dir = QInputDialog::getText(this, "Dossier", "Dossier contenant les fichiers a partager");
    directory=dir.toStdString();
}



void MainWindow::on_descButton_clicked()
{
    string name = ui->fileList->item(ui->fileList->currentRow(),0)->text().toStdString();
    string author = ui->fileList->item(ui->fileList->currentRow(),1)->text().toStdString();
    string *desc = new string (conex->getDesc(name,author));
    DescWindow *descWindow = new DescWindow(this,name,*desc);
    descWindow->exec();
}

void MainWindow::on_upButton_clicked()
{
    vector<FileDescServ>fichiers = conex->getFilesList();
    ui->fileList->setRowCount(0);

    fillGrid(fichiers);
}

void MainWindow::on_searchButton2_clicked()
{
    if(ui->searchEdit->text().isEmpty())
    {
        message.critical(0,"Erreur","Requete vide");
    }
    else
    {
        string mot = ui->searchEdit->text().toStdString();
        vector<FileDescServ>fichiers=conex->searchDesc(mot);
        ui->fileList->setRowCount(0);
        fillGrid(fichiers);
    }

}
